import java.util.ArrayList;
import java.util.List;

public class QuadraticEquationSolver {
    private double a;
    private double b;
    private double c;

    public QuadraticEquationSolver(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public List<ComplexNumber> solve() {
        List<ComplexNumber> roots = new ArrayList<>();

        if (a == 0) {
            System.out.println("a不能为0，方程不是一元二次方程。");
            return roots;
        }

        double discriminant = b * b - 4 * a * c;

        if (discriminant > 0) {
            double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            roots.add(new ComplexNumber(root1, 0));
            roots.add(new ComplexNumber(root2, 0));
        } else if (discriminant == 0) {
            double root = -b / (2 * a);
            roots.add(new ComplexNumber(root, 0));
        } else {
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            roots.add(new ComplexNumber(realPart, imaginaryPart));
            roots.add(new ComplexNumber(realPart, -imaginaryPart));
        }

        return roots;
    }

    public static void main(String[] args) {

        double a = 5;
        double b = 5;
        double c = 2;
        QuadraticEquationSolver solver = new QuadraticEquationSolver(a, b, c);
        List<ComplexNumber> roots = solver.solve();

        System.out.println("方程的根:");
        for (ComplexNumber root : roots) {
            System.out.println(root);
        }
    }
}

class ComplexNumber {
    private double real;
    private double imaginary;

    public ComplexNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    @Override
    public String toString() {
        if (imaginary == 0) {
            return real + "";
        } else if (real == 0) {
            return imaginary + "i";
        } else {
            return real + " + " + imaginary + "i";
        }
    }
}
